package edu.upc.clase.demo.dao.impl;

import edu.upc.clase.demo.dao.SalaDao;
import edu.upc.clase.demo.entity.Sala;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.stereotype.Repository;

/**
 *
 * @author gian
 */
@Repository
public class SalaDaoImpl extends SimpleJdbcDaoSupport implements SalaDao {

    private static Logger log = LoggerFactory.getLogger(SalaDaoImpl.class);

    @Autowired
    public SalaDaoImpl(DataSource dataSource) {
        log.info("Asignando el dataSource");
        setDataSource(dataSource);
    }

    @Override
    public Integer insertar(Sala sala) {
        return insertar(sala, true);
    }
    
    public Integer insertar(Sala sala, boolean insertarHijos) {
        getJdbcTemplate().update(
                "insert into sala (sala, id_local, direccion, capacidad, ind_Amplificador, ind_Parlantes, ind_Aire_acond, costo_sala) values (?, ?, ?, ?, ?, ?, ?, ?)",
                sala.getSala(), sala.getIdLocal(), sala.getDireccion(), sala.getCapacidad() , sala.isIndAmplificador() , sala.isIndParlantes() , sala.isIndAireacond() , sala.getCostoSala());
        return getSimpleJdbcTemplate().queryForInt("select last_insert_id()");
        /*
        int id = getSimpleJdbcTemplate().queryForInt("select last_insert_id()");
        if (insertarHijos){
            getJdbcTemplate().batchUpdate(null, null)
        }
        return id;
        * */
    }

    @Override
    public void actualizar(Sala sala) {
        getJdbcTemplate().update(
                "update sala set sala = ?, direccion = ?, costo_sala = ? where id_sala = ?",
                sala.getSala(), sala.getDireccion() , sala.getCostoSala(), sala.getIdSala());
    }

    @Override
    public void eliminar(Sala sala) {
        getJdbcTemplate().update(
                "delete from sala where id_sala = ?", sala.getIdSala());
    }

    @Override
    public Sala buscar(Integer idSala) {
        try {
            return getSimpleJdbcTemplate().queryForObject(
                    "select id_Sala, sala, id_local, direccion, capacidad, ind_Amplificador, ind_Parlantes, ind_Aire_acond, costo_Sala from sala where id_Sala = ?",
                    new BeanPropertyRowMapper<Sala>(Sala.class), idSala);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    @Override
    public List<Sala> buscarTodos() {
        return getSimpleJdbcTemplate().query(
                "select id_Sala, sala, id_local, direccion, capacidad, ind_Amplificador, ind_Parlantes, ind_Aire_acond, costo_Sala from sala",
                new BeanPropertyRowMapper<Sala>(Sala.class));
    }

    @Override
    public Sala buscar(String sala) {
        try {
            return getSimpleJdbcTemplate().queryForObject(
                    "select id_Sala, sala, id_local, direccion, capacidad, ind_Amplificador, ind_Parlantes, ind_Aire_acond, costo_Sala from sala where sala=?",
                    new BeanPropertyRowMapper<Sala>(Sala.class), sala);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    @Override
    public List<Sala> buscarPorNombre(String sala) {
        try {
            Map<String,String> parametros = new HashMap<String,String>();
            parametros.put("sala","%"+sala+"%");
            return getSimpleJdbcTemplate().query(
                    "select * from sala where sala like :sala",
                    new BeanPropertyRowMapper<Sala>(Sala.class),parametros);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }
}